<template>
  <div class="system-notice">
    <p class="title">
      <img src="/static/images/icon/icon_announcement.png"/>
      <span>{{$t('workbench.systemNotice')}}</span>
      <!-- 增加字段是否为从首页跳转 -->
      <router-link  v-if="isMyAnnouncement" :to="{ path: '/myAnnouncement'}" class="more-link secondary-text">
        <span>{{$t('message.more')}}</span><img src="/static/images/icon/icon_more.png" />
      </router-link>
    </p>
    <div class="announcement-list" :class="{'announcement-list-height': isAdaptive}">
      <ul>
        <li class="announcement-info" :class="{'li-announcement-info': isAdaptive}" v-for="(info,index) in systemNoticeList"
        :key="index" @click="noticeDetails(info.id)">
          <span class="order-wrapper">
            {{index + 1}}
          </span>
          <img class="topping" v-if="info.topStatus === '1'" src="/static/images/icon/icon_topping.png" />
          <span class="info-content">{{info.title}}</span>
          <span class="date">{{info.startTime}}</span>
        </li>
      </ul>
    </div>
    <!-- 系统公告详情 -->
    <edu-dialog
      width="1000px"
      class="systemDialog"
      @confirm="confirmMsg"
      confirmBtnText="确定"
      :isCustomTitle="true"
      :showCancelBtn=false
      :autoHeight=false
      :visible.sync="showDetail">
      <span slot="title">
          <img class="title-img" src="/static/images/icon/icon_title.png" /><span class="title-span">{{systemNoticeDetails.title}}</span>
      </span>
      <div class="info">
        <div class="info-date">{{$t('workbench.releaseDate')}}{{systemNoticeDetails.startTime}}</div>
        <div class="info-content" v-html="systemNoticeDetails.content"></div>
        <div class="info-attach">
            <div v-for="(attach,index) in systemNoticeDetails.commonAttachmentList" :key="index">
                <div class="attach-item" v-if="attach.fileName" @click="handleDownloadfile(attach.fileLacation, attach.fileName)">
                    <i class="el-icon-document"></i>
                    <span class="attach-name">{{attach.fileName}}</span>
                </div>
            </div>
        </div>
      </div>
    </edu-dialog>


    <!-- 弹屏消息展示 -->
    <edu-dialog
      ref="systemNoticeDialog"
      :isCustomTitle="true"
    >
      <span slot="title" v-for=" (titleItem, $index) in popMessageList" :key="$index">
        <template v-if="msgIndex === $index">
          <img class="title-img" src="/static/images/icon/icon_title.png" /><span class="title-span">{{titleItem.title}}</span>
        </template>
      </span>
      <div class="info" v-for=" (msg, index) in popMessageList" :key="index">
        <template v-if="msgIndex === index">
          <div class="info-date">{{$t('workbench.releaseDate')}}{{msg.startTime | formatDate}}</div>
          <div class="info-content" v-html="msg.content"></div>
          <div class="info-attach">
              <div v-for="(attach,index) in msg.commonAttachmentList" :key="index">
                  <div class="attach-item" v-if="attach.fileName" @click="handleDownloadfile(attach.fileLacation, attach.fileName)">
                      <i class="el-icon-document"></i>
                      <span class="attach-name">{{attach.fileName}}</span>
                  </div>
              </div>
          </div>
        </template>
      </div>
      <span slot="buttons">
        <span class="button-left float-left">
          <el-checkbox v-model="checked">{{$t('workbench.notPrompt')}}</el-checkbox>
        </span>
        <span class="float-right">
          <span>{{msgIndex + 1}} / <span> {{popMessageList.length}} </span></span>
          <el-button v-if="msgIndex !== 0" type="primary" @click="pre">{{$t('workbench.previous')}}</el-button>
				  <el-button v-if="(msgIndex + 1) !== popMessageList.length" type="primary" @click="next">{{$t('workbench.next')}}</el-button>
        </span>
        <div class="clear-both"></div>
      </span>
    </edu-dialog>
    <Bombscreen />
  </div>
</template>
<script>
import {
  getPublicMsgDetail
} from "common/src/api/public/information"
import { downloadfile } from "common/src/api/upload"
import Bombscreen from "./Bombscreen.vue";
import filters from "common/src/filters"
import Bus from "common/src/utils/bus";

export default {
  name: "SystemNotice",
  props: {
    systemNoticeList: {
      type: Object,
      default: []
    },
    popMessageList: {
      type: Object,
      default: []
    },
    isAdaptive: {
      type: Boolean
    }
  },
  components: {
    Bombscreen
  },
  data() {
    return {
      showDetail: false,
      systemNoticeDetails: '',  // 公告详情、
      isSystemNoticeMenus: true, // 是否有系统公告菜单
      systemNoticePath: '/systemInfomation', // 系统公告path
      isMyAnnouncement:true,//是否有我的公告菜单
      myAnnouncementPath:'/myAnnouncement',//我的公告菜单路径
      msgIndex: 0  // 第几条弹窗消息
    };
  },
  created() {
    // 处理判断系统公告菜单是否配置
    Bus.$on('getMenusList', list => {
      this.isMyAnnouncement = list.some(item=>{
        return item.urlPath == this.myAnnouncementPath
      })
      this.isSystemNoticeMenus = list.some((item, index) => {
          return item.urlPath === this.systemNoticePath;
      })
      Bus.$emit('isShowNotice', this.isSystemNoticeMenus);
    });
  },
  beforeDestroy() {
    Bus.$off('getMenusList');
  },
  mounted() {
    // 弹窗消息存在时 自动弹窗
    if (this.popMessageList.length) {
      this.$refs.systemNoticeDialog.open();
    }
  },
  methods: {
    // 系统公告详情
    noticeDetails(id) {
      getPublicMsgDetail(id).then(res => {
        this.systemNoticeDetails = res.data;
        this.systemNoticeDetails.startTime = this.formatTime(this.systemNoticeDetails.startTime);
        // 显示查看信息
        this.showDetail = true;
      });
    },
    formatTime(date) {
      return date.slice(0, 10).replace(/-/g, '/');
    },
    // 处理下载文件
    handleDownloadfile(objectkey, filename) {
      if (!objectkey) return;
      downloadfile(filename ? objectkey + `&realName=${filename}` : objectkey);
    },
    // 下一条消息
    next() {
      if (this.msgIndex + 1 < this.popMessageList.length) {
        this.msgIndex ++;
      }
    },
    // 上一条消息
    pre() {
      if (this.msgIndex > 0) {
        this.msgIndex --;
      }
    },
    confirmMsg() {
      this.showDetail = false;
    }
  },
  filters: {
    formatDate(date) {
      return filters.formatDate(date, "yyyy/MM/dd hh:mm:ss");
    }
  }
};
</script>
<style lang="scss" scoped>
  .system-notice {
    // padding: 24px;
    height: 100%;
    overflow: hidden;
    .title {
      font-size: 18px;
      color: #2B3B4E;
      letter-spacing: 0;
      line-height: 24px;
      font-weight: bolder;
      margin: 0;
      img {
        width: 24px;
        height: 24px;
        vertical-align: middle;
      }
      span {
        vertical-align: middle;
      }
      .more-link {
        float: right;
        font-size: 14px;
        color: #3A7FE8;
        span {
          vertical-align: middle;
        }
        img {
          width: 12px;
          height: 12px;
          vertical-align: middle;
        }
      }
    }
    .announcement-list {
      margin-top: 4px;
      height: calc(100% - 28px);
      &.announcement-list-height{
        margin-top: 16px;
        height: calc(100% - 40px);
      }
      ul {
        height: 100%;
      }
      ul, li {
        list-style: none;
        margin: 0;
        padding: 0;
      }
      &::-webkit-scrollbar {/*滚动条整体样式*/
        width: 6px;
        height: 1px;
      }
      &::-webkit-scrollbar-thumb {/*滚动条里面小方块*/
        border-radius: 3px;
        -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
        background: #D4D8DC;
      }
      &::-webkit-scrollbar-track {/*滚动条里面轨道*/
        -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
        border-radius: 10px;
        background: #fff;
      }
      .announcement-info {
        height: 36px;
        line-height: 36px;
        font-size: 14px;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
        &.li-announcement-info {
          height: calc(100% / 7);
          line-height: 100%;
        }
        &:hover {
          color: #3A7FE8;
          cursor: pointer;
          .order-wrapper {
            color: #FFFFFF;
            background: #3A7FE8;
          }
          .date {
            color: #3A7FE8;
          }
        }
        &:first-child, &:nth-child(2), &:nth-child(3) {
          .order-wrapper {
            color: #FFFFFF;
            background: #3A7FE8;
          }
        }
        .order-wrapper {
          display: inline-block;
          width: 16px;
          height: 16px;
          line-height: 16px;
          font-size: 12px;
          text-align: center;
          border-radius: 50%;
          color: #2A3B4F;
          background: #E5E9EF;
          margin-right: 8px;
        }
        .topping {
          margin-right: 4px;
          width: 16px;
          height: 16px;
          vertical-align: middle;
        }
          // @media (min-width: 1920px) {
          //   height: 36px;
          //   line-height: 36px;
          //   .topping {
          //       height: 36px;
          //       line-height: 36px;
          //       img {
          //         margin: 10px 0;
          //       }
          //     }
          // }
        .info-content {
          // display: inline-block;
          flex: 1;
          vertical-align: middle;
          padding-right: 50px;
          // width: 400px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .date {
          // float: right;
          color: #8896A7;
        }
      }
    }
    .title-img {
      width: 24px;
      height: 24px;
      vertical-align: middle;
    }
    .title-span {
      margin-left: 8px;
      font-size: 18px;
      color: #2B3B4E;
      vertical-align: middle;
    }
    .info {
      .info-content {
        margin-top: 15px;
        max-width: 100%;
      }
      .info-attach {
          padding: 10px 0;
          .attach-item {
              display: inline-block;
              padding: 3px 0;
              .attach-name {
                  margin-left: 5px;
                  cursor: pointer;
              }
              &:hover {
                  .attach-name {
                      color: #409eff;
                  }
              }
          }
      }
    }
  }
</style>
<style lang="scss">
.system-notice {
  .el-dialog {
    height: 600px;
    overflow: hidden;
    .el-dialog__body {
      max-height: 462px;
      overflow: auto;
    }
  }
}
.system-notice .systemDialog .el-dialog {
  height: unset;
}
</style>
